/*
 * TLog.c
 *
 *  Created on: Jan 13, 2012
 *      Author: xhu
 */
#include <sys/msg.h>// boite aux lettres
#include <stdio.h>
#include  <stdlib.h>
#include <unistd.h>// sleep
#include "TLog.h"
#include "config.h"

#define TEMPO 2

void TLog(int bal_historique)
{
	int run;
	int lengthMsg;
	MsgHistorique msgr;
	run=1;
	lengthMsg=sizeof(MsgHistorique)-sizeof(long);
	printf("Tlog :start\n");
	while(run)
	{
		int result;
		//printf("Tlog : motor\n");
		result = msgrcv(bal_historique, (void *) &msgr,lengthMsg,0, MSG_NOERROR | IPC_NOWAIT);
		if(result<lengthMsg)
		{
			//perror ("TLog :msgrcv pb for bal_historique");
		}
		else
		{
			printf("TLog : receive msg text : %s \n",msgr.mtext);
			if(msgr.type==4)//fin
			{
				run=0;
			}
			else
			{
				//TODO écrire fichier
			}
		}
		sleep(TEMPO);
	}
	printf("TLog :********************************** end\n");
	exit(0);
}
